package com.mall.server.service.sms;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mall.autoconfig.exception.BusinessException;
import com.mall.dubbo.api.SmsHomeRecommendProductApi;
import com.mall.model.domain.SmsHomeRecommendProduct;
import com.mall.model.vo.CommonPage;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Random;

@Service
public class SmsHomeRecommendProductService {

    @DubboReference
    private SmsHomeRecommendProductApi smsHomeRecommendProductApi;

    //分页查询推荐
    public CommonPage list(Integer pageNum, Integer pageSize, SmsHomeRecommendProduct recommendProduct) {

        String productName = recommendProduct.getProductName();
        Integer recommendStatus = recommendProduct.getRecommendStatus();

        Page<SmsHomeRecommendProduct> page = smsHomeRecommendProductApi.listPage(pageNum, pageSize, productName, recommendStatus);

        return new CommonPage(page);
    }

    //添加首页推荐
    public void create(List<SmsHomeRecommendProduct> homeRecommendProductList) {

        for (SmsHomeRecommendProduct recommendProduct : homeRecommendProductList) {
            Random r = new Random();
            int sort = r.nextInt(100) + 50;
            recommendProduct.setSort(sort);
            recommendProduct.setRecommendStatus(1);
        }

        smsHomeRecommendProductApi.saveBatch(homeRecommendProductList);
    }

    //批量删除推荐
    public void delete(List<Long> ids) {

        smsHomeRecommendProductApi.removeByIds(ids);
    }

    //批量修改推荐状态
    public void update(List<Long> ids, SmsHomeRecommendProduct recommendProduct) {

        List<SmsHomeRecommendProduct> list = smsHomeRecommendProductApi.findByIds(ids, recommendProduct);

        if (!CollUtil.isEmpty(list)) {
            throw new BusinessException("这是错误的操作");
        }
        smsHomeRecommendProductApi.updateByIds(ids, recommendProduct);
    }

    //修改推荐排序
    public void updateSort(Integer id, SmsHomeRecommendProduct recommendProduct) {

        smsHomeRecommendProductApi.updateSort(id, recommendProduct);
    }

}
